package top.honlnk.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.honlnk.common.constant.UserConstant;
import top.honlnk.common.exception.CustomException;
import top.honlnk.pojo.dto.SubmitIdValidateDTO;
import top.honlnk.pojo.dto.UploadRequestDTO;
import top.honlnk.pojo.dto.UploadTableDTO;
import top.honlnk.pojo.dto.UserUpdateTableDTO;
import top.honlnk.pojo.vo.CondensedDataVO;
import top.honlnk.common.result.Result;
import top.honlnk.pojo.vo.SubmitIdValidationResultVO;
import top.honlnk.service.UserService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class userController {

    @Autowired
    private UserService userService;

    @PostMapping("/upload_img")
    public Result<CondensedDataVO> upload(UploadRequestDTO uploadRequestDTO, HttpServletRequest request) {
        // 再请求头中获取key
        String key = request.getHeader("key");

        MultipartFile file = uploadRequestDTO.getFile();
        if (file.isEmpty()) {
            throw new CustomException(UserConstant.UPLOAD_FAIL_NO_FILE);
        }
//      只允许上传 jpg、png、jpeg格式的图片
        String contentType = file.getContentType();
        if (contentType != null && !contentType.equals("image/jpeg") && !contentType.equals("image/png") && !contentType.equals("image/jpg")) {
            throw new CustomException(UserConstant.UPLOAD_FAIL_WRONG_FORMAT);
        }
        if (file.getSize() > UserConstant.MAX_FILE_SIZE) {
            log.error(UserConstant.FILE_SIZE, file.getSize());
            throw new CustomException(UserConstant.UPLOAD_FAIL_OVER_SIZE);
        }
        // 判断name不能为空
        if (uploadRequestDTO.getName().isEmpty()) {
            throw new CustomException(UserConstant.NAME_IS_NULL);
        }

        CondensedDataVO condensedDataVO = userService.upload(file, key, uploadRequestDTO.getName());
        return Result.success(condensedDataVO);
    }

    @PostMapping("/upload_table")
    public Result<CondensedDataVO> uploadTable(@RequestBody UploadTableDTO uploadTableDTO, HttpServletRequest request) {
        // 再请求头中获取key
        String key = request.getHeader("key");

        // 判断name不能为空
        if (uploadTableDTO.getName().isEmpty()) {
            throw new CustomException(UserConstant.NAME_IS_NULL);
        }
        // 二维数组的长和宽都必须等于5，不多不少
        String[][] condensedArray = uploadTableDTO.getTable();
        if (condensedArray.length != 5) {
            for (String[] strings : condensedArray)
                if (strings.length != 5)
                    throw new CustomException(UserConstant.UPLOAD_TABLE_ERROR);
        }
        CondensedDataVO condensedDataVO = userService.uploadTable(uploadTableDTO, key);
        return Result.success(condensedDataVO);
    }

    @GetMapping("/get_table")
    public Result<CondensedDataVO> getTable(HttpServletRequest request) {
        String submitId = request.getHeader("submitId");
        return Result.success(userService.getTable(submitId));
    }

    @PutMapping("/update_table")
    public Result<String> updateTable(@RequestBody UserUpdateTableDTO userUpdateTableDTO, HttpServletRequest request) {
        String submitId = request.getHeader("submitId");
        // 检查上传的表格是否有效（是否为空
        if (userUpdateTableDTO.getTable() == null)
            throw new CustomException(UserConstant.TABLE_IS_NULL);
        // 修改课程表
        userService.setTable(userUpdateTableDTO, submitId);
        return Result.success();
    }

    @PostMapping("/submit_ids/validate")
    public Result<List<SubmitIdValidationResultVO>> validateSubmitIdList(@RequestBody SubmitIdValidateDTO submitIdValidateDTO){
        List<String> submitIds = submitIdValidateDTO.getSubmitIds();
        submitIds.forEach(id -> {
            if (id.length() != 9) {
                throw new CustomException(id + UserConstant.SUBMIT_ID_LENGTH_ERROR);
            }
        });
        return Result.success(userService.validateSubmitIdList(submitIds));
    }
}
